package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.JobInfo;
import com.bw.entity.JobInfoAdd;
import com.bw.entity.JobQuery;
import com.bw.mapper.JobInfoMapper;
import com.bw.service.JobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {


    @Autowired
    JobInfoMapper jobInfoMapper;

    @Autowired
    JobService jobService;

    @RequestMapping("/list.do")
    public String list(JobQuery query, Integer pageNum, Model model) {
        // 下拉框
        selectData(model);
        // 分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 10);
        List<JobInfo> list = jobInfoMapper.selectList(query);
        PageInfo<JobInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        // 条件回显

        model.addAttribute("query", query);
        return "list";
    }


    @RequestMapping("del.do")
    public String del(String id) {
        jobInfoMapper.delById(id);
        return "redirect:list.do";
    }

    /**
     * 添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("addPage.do")
    public String addPage(Model model) {
        // 下拉框
        selectData(model);
        return "add";
    }

    /**
     * 执行添加
     *
     * @return
     */
    @RequestMapping("add.do")
    public String add(@Valid JobInfoAdd jobInfoAdd, BindingResult result, Model model) {
        //1. 非空验证

        if (result.hasErrors()) {
            model.addAttribute("error", result.getAllErrors().get(0).getDefaultMessage());
            // 下拉框
            selectData(model);
            return "add";
        }

        //2. 员工编号以“GH”开始
        String num = jobInfoAdd.getNum();

        if (!num.startsWith("GH")){
            model.addAttribute("error", "员工编号以“GH”开始");
            // 下拉框
            selectData(model);
            return "add";
        }
        // 3. 开始时间小于结束时间
        String startTime = jobInfoAdd.getStartTime();
        DateTime parseStart = DateUtil.parse(startTime, "yyyy-MM-dd");
        String endTime = jobInfoAdd.getEndTime();
        DateTime parseEnd = DateUtil.parse(endTime, "yyyy-MM-dd");

        if (parseStart.isAfter(parseEnd)){
            model.addAttribute("error", "开始时间小于结束时间");
            // 下拉框
            selectData(model);
            return "add";
        }

        //执行添加 保存方法事务，保证原子性
        jobService.add(jobInfoAdd);

        return "redirect:list.do";
    }


    private void  selectData(Model model){
        List<Dict> postList = jobInfoMapper.listPost();
        model.addAttribute("postList", postList);
        List<Dict> schoolList = jobInfoMapper.listSchool();
        model.addAttribute("schoolList", schoolList);
    }
}
